<template>
  <el-container>
    <!--头部区域-->
    <el-header class="homeHeader">
      <div class="title">云E办</div>

      <div>
        <el-button @click="goChat" size="normal" style="color: black;margin-right: 8px" type="text" icon="el-icon-bell"></el-button>
        <!--用户头像-->
        <el-dropdown @command="commendHandler" class="userInfo">
        <span class="el-dropdown-link">
          {{ user.name }}<i><img :src="user.userFace" alt="用户头像"></i>
        </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="adminInfo">个人中心</el-dropdown-item>
            <el-dropdown-item command="setting">设置</el-dropdown-item>
            <el-dropdown-item command="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>

    </el-header>

    <!--侧边栏区域-->
    <el-container>

      <el-aside width="200px">
        <!--根据路由动态渲染页面的导航栏，过滤登录路由hidden-->
        <el-menu router unique-opened>
          <el-submenu :index="index + ''"
                      v-if="!item.hidden"
                      v-for="(item, index) in routers"
                      :key="index">

            <!--菜单标题-->
            <template slot="title">
              <i :class="item.iconCls" style="color: #1accff;margin-right: 5px"></i>
              <span>{{ item.name }}</span>
            </template>

            <!--子菜单-->
            <el-menu-item :index="children.path"
                          v-for="(children, index) in item.children"
                          :key="index">
              {{ children.name }}</el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!--主题区域-->
      <el-main>
        <!--面包屑-->
        <el-breadcrumb separator-class="el-icon-arrow-right" v-if=" this.$router.currentRoute.path !== '/home' ">
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <!--获取当前路基-->
          <el-breadcrumb-item>{{ this.$router.currentRoute.name }}</el-breadcrumb-item>
        </el-breadcrumb>
        <div class="homeWelcome" v-if=" this.$router.currentRoute.path === '/home' ">
          欢迎来到云E办系统！
        </div>
        <!-- 问题有两个路由默认跳转到App的view, 要将路由配置到/home的子路由下-->
        <router-view class="homeRouterView"></router-view>
      </el-main>

    </el-container>

  </el-container>
</template>

<script>
export default {
  name: "Home",
  data(){
    return {
      //从sessionStorage中获取用户信息
      // user: JSON.parse(window.sessionStorage.getItem('user'))
    }
  },
  methods: {
    //头像下拉框处理事件
    commendHandler(commend){
      //处理退出登录
      if (commend === 'logout'){
        this.$confirm('此操作将注销登录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //退出登录后清空token和user信息，跳转登录页面，一定要清空Vuex内存中的菜单，否则第二个登录的用户可以查看所有的菜单信息，刷新后才会返回当前用户的菜单
          this.postRequest('/logout')
          window.sessionStorage.removeItem('token')
          window.sessionStorage.removeItem('user')
          //清空内存中的菜单信息
          this.$store.commit('initRoutes', [])

          //跳转登录页面
          this.$router.replace('/')
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          });
        });
      }
      if (commend == 'adminInfo'){
        this.$router.push('/admininfo')

      }
    },
    //跳转到聊天页面
    goChat(){
      this.$router.push('/chat')
    }
  },
  computed: {
    routers(){
      //获取Vuex中的路由信息
      return this.$store.state.routes
    },
    user(){
      //获取用户信息
      return this.$store.state.currentAdmin
    }
  }
}
</script>

<style scoped>
.homeHeader {
  background: #409eff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 15px;
  box-sizing: border-box;
}
.homeHeader .title {
  font-size: 38px;
  font-family: 楷体;
  color: #ffffff;
}
.userInfo {
  cursor: pointer;
}
.el-dropdown-link img {
  width: 48px;
  height: 48px;
  border-radius: 24px;
  margin-left: 8px;
}
.homeWelcome {
  text-align: center;
  font-size: 30px;
  font-family: 华文楷体;
  color: #409eff;
  padding-top: 50px;
}
.homeRouterView {
  margin-top: 10px;
}
</style>
